//两次dp
class Solution {
public:
    int rob(vector<int>& nums) {
        int n = nums.size();
        if (n == 1)
            return nums[0];
        if (n == 2)
            return max(nums[0], nums[1]);

        vector<int> dp(n, 0);

        //不抢第一个
        dp[1] = nums[1], dp[2] = max(nums[1], nums[2]);
        for (int i = 3; i < n; i++)
            dp[i] = max(dp[i - 1], nums[i] + dp[i - 2]);

        int tmp_max = max(dp[n - 1], dp[n - 2]);

        //不抢最后一个
        dp[0] = nums[0], dp[1] = max(nums[0], nums[1]);
        for (int i = 2; i < n - 1; i++)
            dp[i] = max(dp[i - 1], nums[i] + dp[i - 2]);

        int tmp_max_bak = max(dp[n - 1], dp[n - 2]);

        return max(tmp_max, tmp_max_bak);
    }
};